Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ClimateControl.modify re-entrant #24

Merged
merged 1 commit into from
Mar 31, 2017

Conversation

krallin
Copy link
Contributor

@krallin krallin commented Jan 24, 2017

With the lock added in 0.4, it's no longer safe to nest calls
ClimateControl.modify, which is actually a useful feature when e.g.
using ClimateControl in RSpec around { ... } blocks. Doing so
results in a deadlock (stack trace from the spec included in this
commit):

1) Climate control is re-entrant
   Failure/Error: ClimateControl.modify(options, &block)
   ThreadError:
     deadlock; recursive locking
   # ./lib/climate_control/modifier.rb:10:in `process'
   # ./lib/climate_control.rb:10:in `modify'
   # ./spec/acceptance/climate_control_spec.rb:131:in `with_modified_env'
   # ./spec/acceptance/climate_control_spec.rb:116:in `block (3 levels) in <top (required)>'
   # ./lib/climate_control/modifier.rb:31:in `call'
   # ./lib/climate_control/modifier.rb:31:in `run_block'
   # ./lib/climate_control/modifier.rb:13:in `block in process'
   # ./lib/climate_control/modifier.rb:10:in `process'
   # ./lib/climate_control.rb:10:in `modify'
   # ./spec/acceptance/climate_control_spec.rb:131:in `with_modified_env'
   # ./spec/acceptance/climate_control_spec.rb:115:in `block (2 levels) in <top (required)>'
   # ./spec/acceptance/climate_control_spec.rb:149:in `block (2 levels) in <top (required)>'

Since the lock is here to protect against different threads accessing
ClimateControl at the same time, it's safe for a given thread to make
any changes it wants to the environment as long as it holds the locks,
which is what this patch does.


There was a bit of discussion about re-entrancy in #11, but I don't think the fact that ClimateControl will deadlock when nesting calls in the absence of re-entrancy was addressed.

Thanks!

end
end

expect(foo).to eq('bar')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

it "is re-entrant" do
foo = with_modified_env(FOO: 'foo') do
with_modified_env(BAR: 'bar') do
'bar'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -111,6 +111,16 @@
expect(ENV["BAZ"]).to be_nil
end

it "is re-entrant" do
foo = with_modified_env(FOO: 'foo') do
with_modified_env(BAR: 'bar') do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -111,6 +111,16 @@
expect(ENV["BAZ"]).to be_nil
end

it "is re-entrant" do
foo = with_modified_env(FOO: 'foo') do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

With the lock added in 0.4, it's no longer safe to nest calls
`ClimateControl.modify`, which is actually a useful feature when e.g.
using `ClimateControl` in RSpec `around { ... }` blocks. Doing so
results in a deadlock (stack trace from the spec included in this
commit):

```
1) Climate control is re-entrant
   Failure/Error: ClimateControl.modify(options, &block)
   ThreadError:
     deadlock; recursive locking
   # ./lib/climate_control/modifier.rb:10:in `process'
   # ./lib/climate_control.rb:10:in `modify'
   # ./spec/acceptance/climate_control_spec.rb:131:in `with_modified_env'
   # ./spec/acceptance/climate_control_spec.rb:116:in `block (3 levels) in <top (required)>'
   # ./lib/climate_control/modifier.rb:31:in `call'
   # ./lib/climate_control/modifier.rb:31:in `run_block'
   # ./lib/climate_control/modifier.rb:13:in `block in process'
   # ./lib/climate_control/modifier.rb:10:in `process'
   # ./lib/climate_control.rb:10:in `modify'
   # ./spec/acceptance/climate_control_spec.rb:131:in `with_modified_env'
   # ./spec/acceptance/climate_control_spec.rb:115:in `block (2 levels) in <top (required)>'
   # ./spec/acceptance/climate_control_spec.rb:149:in `block (2 levels) in <top (required)>'
```

Since the lock is here to protect against different threads accessing
ClimateControl at the same time, it's safe for a given thread to make
any changes it wants to the environment as long as it holds the locks,
which is what this patch does.
@geoffharcourt geoffharcourt merged commit bd6d7a2 into thoughtbot:master Mar 31, 2017
@geoffharcourt
Copy link
Contributor

@krallin This looks great. I did some testing on my own as well.

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants